<?php

use Workerman\Worker;
use Workerman\Lib\Timer;

// 自动加载类
require_once __DIR__ . '/Libs/Utility.php';
# 加载支付宝RSA2
require_once __DIR__ . '/Libs/Pays/aop/AopClient.php';
require_once __DIR__ . '/Libs/Pays/aop/request/AlipayTradeAppPayRequest.php';
require_once __DIR__ . '/Libs/Pays/aop/request/AlipayTradeWapPayRequest.php';
require_once __DIR__ . '/Libs/Pays/aop/request/AlipayTradePagePayRequest.php';
# 加载微信支付
require_once __DIR__ . '/Libs/Pays/wx/WxPay.Api.php';
require_once __DIR__ . '/Libs/Pays/wx/WxPay.Data.php';
# 加载二维码生成库
require_once __DIR__ . "/Libs/Qrcode.lib.php";

// 开启的端口
$worker = new Worker('JsonNL://0.0.0.0:2015');
// 启动多少服务进程
$worker->count = 16;
// worker名称，php start.php status 时展示使用
$worker->name = 'ChatJsonRpc';

$worker->onWorkerStart = function($worker)
{
    // 将db实例存储在全局变量中(也可以存储在某类的静态成员中)
    global $db;
    global $redis;
    global $utility;
    $db = \GatewayWorker\Lib\Db::instance('db');
    $redis = new \Redis();
    $redis->connect(\Config\Redis::$redis['host'], \Config\Redis::$redis['port']);

    if(isset(Config\Redis::$redis['auth'])) {
        $redis->auth(Config\Redis::$redis['auth']);
    }
    $utility = new jsonUtility($db, $redis);
};

$worker->onMessage = function($connection, $data)
{
    global $db;
    global $redis;
    global $utility;

    $statistic_address = 'udp://127.0.0.1:55656';
    // 判断数据是否正确
    if(empty($data['class']) || empty($data['method']) || !isset($data['param_array']))
    {
        // 发送数据给客户端，请求包错误
        return $connection->send(array('code'=>400, 'msg'=>'bad request', 'data'=>null));
    }
    // 获得要调用的类、方法、及参数
    $class = $data['class'];
    $method = $data['method'];
    $param_array = $data['param_array'];

    // $_SESSION['remoteip'] = ip2long($connection->getRemoteIp());

    //JsonClient::tick($class, $method);
    $success = false;
    // 判断类对应文件是否载入
    if(!class_exists($class))
    {
        $include_file = __DIR__ . "/Services/$class.php";
        if(is_file($include_file))
        {
            require_once $include_file;
        }
        if(!class_exists($class))
        {
            $code = 404;
            $msg = "class $class not found";
            //JsonClient::report($class, $method, $success, $code, $msg, $statistic_address);
            // 发送数据给客户端 类不存在
            return $connection->send(array('state'=>$code, 'msg'=>$msg, 'data'=>null));
        }
    }

    // 调用类的方法
    try
    {
        $ret = call_user_func_array(array($class, $method), $param_array);
        //JsonClient::report($class, $method, 1, 0, '', $statistic_address);
        // 发送数据给客户端，调用成功，data下标对应的元素即为调用结果
        return $connection->send(array('state'=>200, 'msg'=>'success', 'data'=>$ret));
    }
        // 有异常
    catch(Exception $e)
    {
        // 发送数据给客户端，发生异常，调用失败
        $code = $e->getCode() ? $e->getCode() : 500;
        //JsonClient::report($class, $method, $success, $code, $e, $statistic_address);
        return $connection->send(array('state'=>$code, 'msg'=>$e->getMessage(), 'data'=>$e));
    }

};


// 如果不是在根目录启动，则运行runAll方法
if(!defined('GLOBAL_START'))
{
    Worker::runAll();
}